import { service, crudParamsSerializer } from '@/utils/request'
import { CreateQueryParams } from '@nestjsx/crud-request'
import { Register } from './register.dto'
import { CaptchaChallenge } from '../captcha'
import { User } from './user.dto'
import { CrudPaginatedRes } from '..'
import * as _ from 'lodash'
import { UserRole, UserStatus } from '../enum'

export const defaultUserData: Partial<User> = {
  username: '',
  roles: [UserRole.USER],
  status: UserStatus.ACTIVE
}

export function getUsers (params?: CreateQueryParams) {
  return service.get<User[] | CrudPaginatedRes<User>>(
    '/users',
    {
      params,
      paramsSerializer: crudParamsSerializer
    }
  )
}

export function getUser (id: number | string, params: CreateQueryParams) {
  return service.get<User>(
    `/users/${id}`,
    {
      params,
      paramsSerializer: crudParamsSerializer
    }
  )
}

export function getUserAuthed () {
  return service.get<User>('/auth/me')
}

// export function login (data: {username: string; password: string}, captcha: CaptchaChallenge) {
//   return service.post(
//     '/auth/login',
//     data,
//     {
//       headers: {
//         captcha: captcha.toHeaderString()
//       }
//     }
//   )
// }

export function login (data: {username: string; password: string}) {
  return service.post(
    '/auth/login',
    data
  )
}

export function logout () {
  return service.post<boolean>(
    '/auth/logout'
  )
}

export function register (data: Partial<Register>, captcha: CaptchaChallenge) {
  return service.post('/auth/register',
    data,
    {
      headers: {
        captcha: captcha.toHeaderString()
      }
    }
  )
}

export function createUser (dto: Partial<User>, params?: CreateQueryParams) {
  const data = new FormData()
  _.forIn(dto, (value, key) => {
    if (key !== 'id' && value) {
      if (typeof value === 'string' || typeof value === 'number') {
        data.append(key, value.toString())
      } else if (value instanceof Blob) {
        data.append(key, value)
      } else if (value instanceof Date) {
        data.append(key, value.toISOString())
      } else {
        data.append(key, JSON.stringify(value))
      }
    }
  })
  return service.post<User>(
    '/users',
    data,
    {
      params,
      paramsSerializer: crudParamsSerializer
    }
  )
}

export function updateUser (id: number | string, dto: Partial<User>, params?: CreateQueryParams) {
  const data = new FormData()
  _.forIn(dto, (value, key) => {
    if (key !== 'id' && value) {
      if (typeof value === 'string' || typeof value === 'number') {
        data.append(key, value.toString())
      } else if (value instanceof Blob) {
        data.append(key, value)
      } else if (value instanceof Date) {
        data.append(key, value.toISOString())
      } else {
        data.append(key, JSON.stringify(value))
      }
    }
  })
  return service.patch<User>(
     `/users/${id}`,
     data, {
       params,
       paramsSerializer: crudParamsSerializer
     }
  )
}
